<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Site;
use App\Models\Template;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Admin\Extensions\Tools\Build as BuildButton;
use App\Helper\Build\Build;

class SiteController extends Controller
{
    protected $name = '站点管理';
    use ModelForm;
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
	//生成页面
	public function build()
	{
		$data = request()->all();
		if(empty($data['id']))return ['status'=>0, 'message'=> '请勾选要生成的站点.'];
		
		$ids = explodeId($data['id']);
		
		$list = Site::whereIn('id', $ids)->get();
		
		
		$msg = '';
		foreach($list as $row)
		{
            $Build = new Build($row);
			$arr = $Build->cover();
			$msg .= $arr['msg'].chr(10);
		}
		return ['status'=>1, 'message'=> $msg];
	}
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Site::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            $grid->tools(function ($tools) {
				$tools->append(new BuildButton());
			});
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->filter(function($filter){
                
                $filter->like('name', '标题');
                //$filter->addFilter($filter->like('post_title', '标题'));
                
            
            });
            $grid->column('id', 'ID')->sortable();
            
            $grid->column('name', '名称');
            $grid->column('domain', '域名');
            $grid->column('site_file', '目录');
            
            $grid->column('updated_at', '更新时间');
            // $grid->column('is_oss','是否存在OSS')->options()->select(['1'=>'是',0=>'否']);
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        return Admin::form(Site::class, function (Form $form) {
            
            $form->number('id', 'ID')->attribute('disabled', false);
            $form->text('name', '名称')->rules('required');
            $form->text('domain', '域名')->rules('nullable')->help('例如：www.anhuinews.com');
            $form->radio('is_index','是否首页')->options(['1'=>'是',0=>'否'])->default('0')->help('如果是首页，则下面模版不需要选择');
            $form->select('temp_id','模板')->options(selectOptionsField(Template::where('type', 2)->get(), 'title'));
            $form->text('site_file', '服务器路径')->rules('required')->help('站点绝对目录,以/结束');
            $form->text('site_html', '静态文件目录')->rules('required')->help('相对于站点目录文件位置,以/结束');
            
            $form->embeds('remark', '其他配置', function ($form) {

            });



            $form->radio('is_oss','是否存在OSS')->options(['1'=>'是',0=>'否'])->default('0');
            $form->text('oss', 'oss名称');
        });
    }
}
